package com.educate.member.rpc.provide.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.educate.common.utils.converter.ListUtils;
import com.educate.common.utils.exception.AppException;
import com.educate.device.model.bo.MemberBO;
import com.educate.device.model.po.Member;
import com.educate.member.dao.MemberDao;
import com.educate.member.rpc.api.MemberService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

@Log4j2
@Service(
        version = "${api.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class MemberServiceImpl implements MemberService {

    @Resource MemberDao memberDao;

    @Override
    public void create(MemberBO bo) {
        memberDao.insert(bo);
    }

    @Override
    public void delete(MemberBO bo) {
        memberDao.delete(bo);
    }

    @Override
    public void delete(String memberId) {
        memberDao.deleteById(memberId);
    }

    @Override
    public MemberBO detail(String memberId) {
//        Optional<Member> optional = memberDao.findById(memberId);
        System.out.println("memberId = " + memberId);
//        if (true) {
            throw new AppException("不存在的实例");
//        }
//        return new MemberBO(optional.get());
    }

    @Override
    public MemberBO detail(MemberBO bo) {
//        return detail(bo.getOrganizationId());
        return null;
    }

    @Override
    public void update(MemberBO bo) {
//        Optional<Member> optional = memberDao.findById(bo.getMemberId());
//        if (!optional.isPresent()) {
//            throw new AppException("不存在的实例");
//        }
//        BeanUtils.copyPropertiesNonEmpty(bo, optional.get());
//        memberDao.save(optional.get());
    }

    @Override
    public Page<MemberBO> query(MemberBO bo, PageRequest pageRequest) {
        return memberDao.query(bo, pageRequest).map(MemberBO::new);
    }

    @Override
    public List<MemberBO> list(MemberBO bo) {
        List<Member> result = memberDao.findAll();
        return ListUtils.map(result, MemberBO::new);
    }
}
